import {Component, OnInit} from '@angular/core';
import {AddAftApplyOrderComponent} from '../add-aft-apply-order.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {AftApplyOrder} from '../../aft-apply-order.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';

@Component({
    selector: 'app-main-add-aft-apply-order',
    templateUrl: './main-add-aft-apply-order.component.html',
    styleUrls: ['./main-add-aft-apply-order.component.css']
})
export class MainAddAftApplyOrderComponent implements OnInit {

    errRes = new ErrRes();

    domain = new AftApplyOrder();

    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
                this.domain.serUsing = Zero3Enum.ENABLE;
                this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
            },
            serAudit: {
                nickname: ''
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            serInputGroupData: {
                icon: fa.faCalendar,
                nickname: '日期',
                value: () => {
                    return this.domain.serInputGroupData;
                },
                focus: (event) => {
                    this.domain.serInputGroupData = event.target.value.toString();
                },
                blur: (event) => {
                }
            },
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.aftApplyOrder.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.aftApplyOrder.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addAftApplyOrder: AddAftApplyOrderComponent) {
        this.addAftApplyOrder.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
    }


}
